package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理
 * @author 张敏
 * @date 2020/4/3
 */

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    /**
     * 安全框架security
     * @return
     */
    @RequestMapping("/getUsername")
    public Result getUsername(){
        //认证完成后，security框架会将权限认证时查询数据库的user对象存入域对象中，底层实现为session
        //SecurityContextHolder.getContext():就是获取域对象，可以理解为session
        //getAuthentication()：获取认证信息
        //getPrincipal():获取登录后的认证信息
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(user == null){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
        return new Result(true,MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
    }

    /**
     * 根据条件分页查询用户信息
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByCondition")
    public PageResult findByCondition(@RequestBody QueryPageBean queryPageBean){

        try {
            return userService.findByCondition(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 添加用户
     * @param user
     * @param roleIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] roleIds){

        System.out.println("user ===================== " + user);
        try {
            userService.add(user,roleIds);
            return new Result(true,"新增用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增用户失败");
        }
    }

    /**
     * 根据id查询用户信息
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
           com.itheima.pojo.User user = userService.findById(id);
           return new Result(true,"查询用户成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询用户失败");
        }
    }

    /**
     * 根据用户id查询用户包含的角色信息
     * @param id
     * @return
     */
    @RequestMapping("/findRoleByUserId")
    public Result findRoleByUserId(Integer id){

        try {
            List<Integer> roleIds=userService.findRoleByUserId(id);
            return new Result(true,"查询角色成功",roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"查询角色失败");
        }
    }

    /**
     * 编辑用户信息
     * @param user
     * @param roleIds
     * @return
     */
    @RequestMapping("/edit")
    public Result update(@RequestBody com.itheima.pojo.User user,Integer[] roleIds){
        try {
            userService.edit(user, roleIds);
            return new Result(true, "编辑角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑角色失败");
        }
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try {
            userService.deleteById(id);
            return new Result(true, "删除用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除用户失败");
        }
    }
}
